<?php

/**
 * This is the model class for table "{{receivedetails}}".
 *
 * The followings are the available columns in table '{{receivedetails}}':
 * @property string $idreceivedetails
 * @property string $idreceive
 * @property string $idstockitems
 * @property string $receivequantity
 * @property string $receiverate
 * @property string $receivevalue
 * @property string $itemquantitytested
 * @property string $itemquantityback
 * @property string $testreport
 * @property string $remark
 * @property string $testdate
 * @property string $sixsigmano
 *
 * The followings are the available model relations:
 * @property Receive $idreceive0
 * @property Stockitems $idstockitems0
 */
class Receivedetails extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Receivedetails the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{receivedetails}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idreceive, idstockitems', 'required'),
			array('idreceive, idstockitems, receivequantity, receiverate, receivevalue, itemquantitytested, itemquantityback', 'length', 'max'=>10),
			array('testreport', 'length', 'max'=>50),
			array('remark', 'length', 'max'=>100),
			array('sixsigmano', 'length', 'max'=>20),
			array('testdate', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idreceivedetails, idreceive, idstockitems, receivequantity, receiverate, receivevalue, itemquantitytested, itemquantityback, testreport, remark, testdate, sixsigmano', 'safe', 'on'=>'search'),
                        array('invoiceno', 'autocompleteValidator', 'model'=>'Receive'),
                        array('stockitemsname', 'autocompleteValidator', 'model'=>'Stockitems'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!$params['model']::model()->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['invoiceno'])){
                $match = addcslashes($_REQUEST['invoiceno'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'invoiceno = :invoiceno',
                    'params'    => array(':invoiceno' => "$match")
                ) );            
                $model=Receive::model()->find($criteria);
                $this->idreceive = $model->idreceive;
            }
                        if(isset($_REQUEST['stockitemsname'])){
                $match = addcslashes($_REQUEST['stockitemsname'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'stockitemsname = :stockitemsname',
                    'params'    => array(':stockitemsname' => "$match")
                ) );            
                $model=Stockitems::model()->find($criteria);
                $this->idstockitems = $model->idstockitems;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idreceive0' => array(self::BELONGS_TO, 'Receive', 'idreceive'),
			'idstockitems0' => array(self::BELONGS_TO, 'Stockitems', 'idstockitems'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idreceivedetails' => Yii::t('application', 'Idreceivedetails'),
			'idreceive' => Yii::t('application', 'Idreceive'),
			'idstockitems' => Yii::t('application', 'Idstockitems'),
			'receivequantity' => Yii::t('application', 'Receivequantity'),
			'receiverate' => Yii::t('application', 'Receiverate'),
			'receivevalue' => Yii::t('application', 'Receivevalue'),
			'itemquantitytested' => Yii::t('application', 'Itemquantitytested'),
			'itemquantityback' => Yii::t('application', 'Itemquantityback'),
			'testreport' => Yii::t('application', 'Testreport'),
			'remark' => Yii::t('application', 'Remark'),
			'testdate' => Yii::t('application', 'Testdate'),
			'sixsigmano' => Yii::t('application', 'Sixsigmano'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idreceivedetails',$this->idreceivedetails,true);
		$criteria->compare('idreceive',$this->idreceive,true);
		$criteria->compare('idstockitems',$this->idstockitems,true);
		$criteria->compare('receivequantity',$this->receivequantity,true);
		$criteria->compare('receiverate',$this->receiverate,true);
		$criteria->compare('receivevalue',$this->receivevalue,true);
		$criteria->compare('itemquantitytested',$this->itemquantitytested,true);
		$criteria->compare('itemquantityback',$this->itemquantityback,true);
		$criteria->compare('testreport',$this->testreport,true);
		$criteria->compare('remark',$this->remark,true);
		$criteria->compare('testdate',$this->testdate,true);
		$criteria->compare('sixsigmano',$this->sixsigmano,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}